@isTest
private class AssignmentGroupTriggerTester
{
    public static testMethod void TestAGTrigger()
    {
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser )
        {
            User test_user = new User();
            
            test_user.UserName = 'xyz_1234567890@abcd.com';
            test_user.LastName = 'Michael Shu Test Email';
            test_user.Email = 'sshu@attinteractive.com';
            test_user.Alias = 'abcde';
            test_user.CommunityNickname = 'xyaz_1234556';
            test_user.TimeZoneSidKey = 'America/Los_Angeles';
            test_user.LocaleSidKey = 'en_US';
            test_user.EmailEncodingKey = 'UTF-8';
            test_user.ProfileId = [select id from Profile where Name like '%IOG Support%' limit 1].Id;
            test_user.UserRoleId = [select id from UserRole limit 1].Id ;
            test_user.LanguageLocaleKey = 'en_US';
            test_user.IsActive = true;
            insert test_user;
            Assignment_Group_Name__c AGN = new Assignment_Group_Name__c();   
             AGN.name = 'AG1';  
             AGN.Function__c = 'TestOrder';     
             AGN.Type__c = 'Cases' ;      
             AGN.Description__c = 'Description';     
             AGN.Round_Robin__c = true;      
             insert AGN;
            
            
            //list<Assignment_Group_Name__c> agns = [select id, Name from Assignment_Group_Name__c limit 1];
            //Assignment_Group_Name__c agn = new Assignment_Group_Name__c(ID = agns[0].ID);
            
            Schema.DescribeFieldResult agResult = Assignment_Groups__c.Cycle_Team__c.getDescribe();
            List<Schema.PicklistEntry> agEntries = agResult.getPicklistValues();
            
            Assignment_Groups__c ag = new Assignment_Groups__c();
            
            ag.Cycle_Team__c = agEntries[0].getValue();
            ag.Group_Name__c = AGN.id;
            ag.User__c = test_user.id;
            
            insert ag;
            
            // validate the ag --> user sync direction
            List<User> user = [select ID, Cycle_Team__c from User where ID = :test_user.Id];
            System.assertEquals(user[0].Cycle_Team__c, ag.Cycle_Team__c);
            
            // validate the user --> ag sync direction
            user[0].Cycle_Team__c = agEntries[1].getValue();
            update user[0];
            
            list<Assignment_Groups__c> ags = [select id, User__c, Cycle_Team__c from Assignment_Groups__c where User__c = :user[0].Id];
            System.assertEquals(user[0].Cycle_Team__c, ags[0].Cycle_Team__c);
        }
    }   
}